import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';

export class AlbumManager {
  context
  Asset: photoAccessHelper.PhotoAsset | undefined
  FileName: string = ''
  pam:  photoAccessHelper.PhotoAccessHelper | undefined
  Fd: number;

  constructor(context) {
    this.context = context
    this.pam = photoAccessHelper.getPhotoAccessHelper(this.context);
  }

  async saveImg(title) {
    let TAG = "_:_saveImg"
    console.log(TAG, "title=", title, "entered")
    try {
      let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE;
      let extension: string = 'jpg';
      let options: photoAccessHelper.CreateOptions = {
        title: title
      }
      let uri: string = await this.pam.createAsset(photoType, extension, options);
      console.info(TAG, 'createAsset uri' + uri);
      console.info(TAG, 'createAsset successfully');
      this.Fd = await this.Asset.open('w')
    } catch (err) {
      console.error(TAG, `createAsset failed, error: ${err.code}, ${err.message}`);
    }
  }

  private async createAsset(): Promise<void> {
  let TAG="_:_createAsset"
    try {
      this.Asset = await this.pam.createAsset(this.FileName)
      this.Fd = await this.Asset.open('w')
      this.pam
    } catch (err) {
      console.error(TAG, 'createAsset error:', JSON.stringify(err))
      this.Asset && this.Asset.close(this.Fd)
      this.Fd = -1
    }
  }

  // async saveBase64ImageToGallery(base64String, fileName) {
  //   try {
  //     // 将Base64字符串转换为Buffer
  //     const buffer = Buffer.from(base64String, 'base64');
  //
  //     // 获取相册的保存路径
  //     const savePath = await photoAccessHelper.getSaveFilePath({
  //       suffix: '.jpg' // 根据实际图片格式设置后缀
  //     });
  //
  //     // 将图片数据写入文件
  //     await photoAccessHelper.write(buffer, savePath);
  //
  //     // 将文件添加到相册
  //     await photoAccessHelper.addImageToGallery(savePath);
  //
  //     console.log('图片保存成功，文件路径：', savePath);
  //     return Promise.resolve(savePath);
  //   } catch (error) {
  //     console.error('图片保存失败：', error);
  //     return Promise.reject(error);
  //   }
  // }
}